package com.sky.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.sky.domain.User;
import com.sky.exception.UserException;
import com.sky.service.UserService;
import com.sky.utils.PassWordutil;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //该map中存储这用户名和密码两个字段的信息
		//1.获取请求参数
		Map<String, String[]> map = request.getParameterMap(); 
		String remember = request.getParameter("remember");
		String autoLogin = request.getParameter("autoLogin");
		User user =new User();
		try {
			//封装请求参数的值到User对象中
			BeanUtils.populate(user, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		加密
		String pwd= "";
		String pwd2=user.getPassword();
		try {
			PassWordutil des = new PassWordutil();
			pwd=des.encrypt(user.getPassword());
			user.setPassword(pwd);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
//		记住密码
		if ("on".equals(remember)) {
			//选中，记住密码
			 request.getSession().setAttribute("pwd", pwd2);
			 request.getSession().setAttribute("flag", 1);
		} else {
			 request.getSession().setAttribute("flag", 0);
			 request.getSession().setAttribute("pwd", "");
		}
		
		//2.处理业务逻辑
		  UserService userService =new UserService();
		  try {
			 //登录成功
			 User user2 = userService.LoginUser(user);
			//登录成功，跳转到程序的首界面
			//登录状态：
			request.getSession().setAttribute("user", user2);
			
//			自动登录
			Cookie cookie =new Cookie("user", user2.getUsername()+"-"+user2.getPassword());
			 if (autoLogin!=null) {
				//表示下次要自动登录
				 cookie.setMaxAge(60*60*24*7);//用户信息保存7天
			  }else{
				//表示下次自己手动登录
				  cookie.setMaxAge(0);
			  }
			 response.addCookie(cookie);
			 request.getSession().setAttribute("name", user2.getUsername());
			//request.setAttribute("user", user);作用范围比较小
			//重定向：地址栏发生变化，请求了2次
			response.sendRedirect("home/home.jsp");
			//请求转发：地址栏不变，请求了一次
			//request.getRequestDispatcher("home.jsp").forward(request, response);
		} catch (SQLException e) {
			//登录失败,刷新登录页面告诉用户登录失败的信息
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("error_msg", e.getMessage());
			request.getRequestDispatcher("login.jsp").forward(request, response);
		} catch (UserException e) {
			// TODO Auto-generated catch block
			//登录失败,刷新登录页面告诉用户登录失败的信息
			e.printStackTrace();
			request.setAttribute("error_msg", e.getMessage());
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		//3.分发转向
		  
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}


}
